package com.alibaba.citrus.cr.order.open.api.facade.service.notifyquantity;

import com.alibaba.citrus.cr.common.exception.FacadeException;
import com.alibaba.citrus.cr.order.open.api.facade.model.FulfillmentOrderData;
import com.alibaba.citrus.cr.order.open.api.facade.model.FulfillmentOrderMessage;
import com.alibaba.citrus.cr.order.open.api.facade.model.TransferOrder;
import com.alibaba.citrus.cr.order.open.api.facade.model.TransferOrderQueryRequest;
import com.alibaba.citrus.cr.order.open.api.facade.transferorder.TransferOrderQueryService;
import com.alibaba.cloud.retail.delivery.api.model.delivery.enums.FulfillmentDetailStatusEnum;
import com.alibaba.cloud.retail.delivery.api.model.delivery.enums.FulfillmentDirectionTypeEnum;
import com.alibaba.cloud.retail.delivery.api.model.delivery.response.fulfillment.FulfillmentDetailGetRes;
import com.alibaba.cloud.retail.delivery.api.model.delivery.response.fulfillment.FulfillmentGetRes;
import com.alibaba.cloud.retail.delivery.api.model.fulfillment.request.SearchFulfullmentOrderReq;
import com.alibaba.cloud.retail.delivery.api.service.bcfulfillment.FulfillmentOrderAbilityService;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.crunifiedinventory.domain.allocateorderdetail.service.AllocateOrderDetailService;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: yuhw
 * @date: 2024年02月29日 10:24
 **/
public interface FulfillmentNotifyQuantityHandler extends NotifyQuantityHandler<FulfillmentOrderData> {

    @Override
    void handle(FulfillmentOrderData fulfillmentOrderData);

    @Override
    String getTransferOrderType();

    default TransferOrder getTransferOrder(FulfillmentOrderData fulfillmentOrderData){
        TransferOrderQueryRequest transferOrderQueryRequest = new TransferOrderQueryRequest();
        transferOrderQueryRequest.setTransferOrderCode(fulfillmentOrderData.getSourceOrderNo());
        Result<TransferOrder> transferOrderResult = getTransferOrderQueryService().query(transferOrderQueryRequest);
        return Optional.ofNullable(transferOrderResult).map(Result::getResult).orElse(null);
    }

    default List<FulfillmentGetRes> getFulfillmentGetRes(FulfillmentOrderData fulfillmentOrderData){
        SearchFulfullmentOrderReq searchFulfullmentOrderReq = new SearchFulfullmentOrderReq();
        searchFulfullmentOrderReq.setSourceOrderNo(fulfillmentOrderData.getSourceOrderNo());
        searchFulfullmentOrderReq.setFulfillmentDirectionType(FulfillmentDirectionTypeEnum.FORWARD.getValue());
        com.alibaba.cloud.retail.delivery.api.model.delivery.response.Result<List<FulfillmentGetRes>> listResult = getFulfillmentOrderAbilityService().searchFulfullmentOrder(searchFulfullmentOrderReq);
        return Optional.ofNullable(listResult).map(com.alibaba.cloud.retail.delivery.api.model.delivery.response.Result::getResult).orElse(Lists.newArrayList());
    }

    FulfillmentOrderAbilityService getFulfillmentOrderAbilityService();

    TransferOrderQueryService getTransferOrderQueryService();

    AllocateOrderDetailService getAllocateOrderDetailService();

    @Deprecated
    default void execute(FulfillmentOrderData request){
        List<FulfillmentGetRes> fulfillmentGetRes = getFulfillmentGetRes(request);
        if (CollectionUtils.isEmpty(fulfillmentGetRes)){
            throw new FacadeException("");
        }
        TransferOrder transferOrder = getTransferOrder(request);
        transferOrder.getDetailList().forEach(transferSubOrder -> {
            //匹配上调拨单的行
            List<FulfillmentDetailGetRes> fulfillmentDetailGetRes = fulfillmentGetRes.stream().filter(res -> CollectionUtils.isNotEmpty(res.getDetailList()))
                    .flatMap(res -> res.getDetailList().stream())
                    .filter(detail -> Objects.equals(transferSubOrder.getLineNo(),detail.getRefOrderLineNo()))
                    .collect(Collectors.toList());
            if (CollectionUtils.isEmpty(fulfillmentDetailGetRes)){
                return;
            }
            BigDecimal fulfillmentQuantity = fulfillmentDetailGetRes.stream().map(FulfillmentDetailGetRes::getQuantity)
                    .filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
            AllocateOrderDetailService allocateOrderDetailService = getAllocateOrderDetailService();
//            allocateOrderDetailService.updateFeatures();
        });
    }

    //排除计算的履约子单状态
    List<String> excludeFulfillmentOrderStatusList = Lists.newArrayList(FulfillmentDetailStatusEnum.WAIT_SUBMIT.getValue(),
            FulfillmentDetailStatusEnum.CANCEL.getValue(),FulfillmentDetailStatusEnum.SUBMIT.getValue())
            .stream().map(String::valueOf).collect(Collectors.toList());
}
